package main

import "fmt"

type Element interface {
	Accept(v Visitor)
}

type Text struct {
	Content string
}

func (t *Text) Accept(v Visitor) {
	v.VisitText(t)
}

type Image struct {
	Url string
}

func (i *Image) Accept(v Visitor) {
	v.VisitImage(i)
}

type Visitor interface {
	VisitText(text *Text)
	VisitImage(image *Image)
}

type PDFVisitor struct {
}

func (p *PDFVisitor) VisitText(text *Text) {
	fmt.Println("pdf 获取文本内容", text.Content)
}

func (p *PDFVisitor) VisitImage(image *Image) {
	fmt.Println("pdf 获取图片url", image.Url)
}

type HTMLVisitor struct {
}

func (h *HTMLVisitor) VisitText(text *Text) {
	fmt.Println("html 获取文本内容", text.Content)
}

func (h *HTMLVisitor) VisitImage(image *Image) {
	fmt.Println("html 获取图片url", image.Url)
}

type Document struct {
	Elements []Element
}

func (d *Document) AddElement(element Element) {
	d.Elements = append(d.Elements, element)
}

func (d *Document) Accept(visitor Visitor) {
	for _, element := range d.Elements {
		element.Accept(visitor)
	}
}

func main() {
	document := &Document{}
	document.AddElement(&Text{Content: "hello world"})
	document.AddElement(&Image{Url: "https://www.baidu.com/me.jpg"})

	document.Accept(&PDFVisitor{})
	fmt.Println("------------------------------------------------------")
	document.Accept(&HTMLVisitor{})
}
